<?php
/**
 * Author: LaiGuiming
 * Time: 2017-07-27 15:08
 * Copyright (C) 2016 Longyan Wehome Message CO.,LTD.
 */

namespace app\admin\controller\sys\dev;
use app\admin\logic\TemplateLogic;
use app\common\controller\BackEnd;
use app\admin\logic\TemplateBootstrap;
use think\Db;
use think\Request;

/**
 * 代码生成控制器
 * Class Generate
 * @package app\admin\controller
 */
class Generate extends BackEnd
{
    private $database_info;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->database_info=config('database'); //数据连接信息
    }

    /**
     * 初始页
     * @return mixed
     */
    public  function init(){
        return $this->fetch();
    }

    /**
     * 数据库列表
     * @return mixed
     */
    public function crud()
    {
        $database=$this->database_info['database'];
        $sql = "select table_name as name,table_comment as comment from INFORMATION_SCHEMA.TABLES Where table_schema = '". $database . "'";
        $tables = Db::connect($this->database_info)->query($sql);

        //去掉没有主键的表
        if ($tables) {
            foreach ($tables as $k => $v) {
                if (!$this->getTableId($v['name'])) {
                    unset($tables[$k]);
                }
            }
        }

        $this->assign('tables', $tables);
        return $this->fetch();
    }

    /**
     * 创建crud代码
     * @param $tables
     */
    public function create($tables)
    {
        //类型 1:CRUD 2:Table
        $type=input("type");
        $items=implode(",", $_POST['item']);

        //字符串切割成数组，多张表
        $table_arr = explode(',', $tables);
        $tables = $this->getTableName($table_arr);

        $err_msg="";//失败信息
        $suc_msg="";//成功信息

        $logic=new TemplateLogic();
        //开始生成
        foreach ($tables as $k => $v) {
            if($type==1){
                //crud
                if(strpos($items,'1')!== false){
                    $logic->createModel($v);
                }
                if(strpos($items,'2')!== false){
                    $logic->createValidate($v);
                }
                if(strpos($items,'3')!== false){
                    $logic->createController($v);
                }
                if(strpos($items,'4')!== false){
                    $logic->createIndexView($v);
                }
                if(strpos($items,'5')!== false){
                    $logic->createAddView($v);
                }
                $suc_msg=$suc_msg.$v['name']."文件已生成！菜单编码/admin/". $v['_name'] ."/index \r\n";
            }else{
                $viewName=input("viewName");
                $url=input("url");
                if(!$viewName){
                    $viewName="test";
                }
                $logic->createReport($viewName,$url);
                $suc_msg=$viewName."报表模板已生成！\r\n";
            }
        }
        $res_msg=$suc_msg.$err_msg;
        $this->resSuccess("",$res_msg);
    }

    /**
     * 得到表的字段信息
     * @param $table_name
     * @return bool
     */
    public function getTableId($table_name)
    {
        $sql = "select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where table_name='{$table_name}' AND COLUMN_KEY='PRI' limit 1";
        $result = Db::connect($this->database_info)->query($sql);

        if (isset($result[0]['COLUMN_NAME'])) {
            return $result[0]['COLUMN_NAME'];
        } else {
            return false;
        }
    }

    /**
     * 数据表名称处理
     * @param $tables
     * @return array|bool
     */
    public function getTableName($tables)
    {
        if (!$tables) return false;
        $arr = array();
        foreach ($tables as $k => $v) {
            $index = strpos($v, '@') + 1;
            $arr[$k]['comment'] = substr($v, $index);
            $v = substr($v, 0, $index - 1);
            $id = $this->getTableId($v);
            if (!$id) {
                continue;
            }
            $arr[$k]['id'] = $id;
            $pos = strpos($v, '_') + 1;
            $arr[$k]['_name'] = substr($v, $pos);
            $arr[$k]['name'] = convertUnderline($arr[$k]['_name']);
            $arr[$k]['prefix'] = substr($v, 0, $pos);
        }
        return $arr;
    }
}